<?php
//严格校验模式
declare(strict_types=1);

namespace app\controller;

use app\BaseController;
use QL\QueryList;

class Index extends BaseController
{
    public function index()
    {
        return view();
    }

    public function data(){
        $data = QueryList::get('https://www.baidu.com/s?wd=李天文', null, [
            'headers' => [
                'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
//                'Accept-Encoding' => 'gzip, deflate, br',  //类型格式
            ]
        ])->rules([
            'title' => ['h3', 'text' ,'body'],
            'link' => ['h3>a', 'href']
        ])
//        ->range('.result')
        ->queryData();
        print_r($data);
    }

    public function data_res(){
        //采集某页面所有的图片
//        $data = QueryList::get('http://cms.querylist.cc/bizhi/453.html')->find('img')->attrs('src');
//        $data = QueryList::get('https://www.php.cn')->find('img')->attrs('src');
//        $data = QueryList::get('https://www.bilibili.com/video/BV1NY4y1A7PU?spm_id_from=333.851.b_7265636f6d6d656e64.1')->find('img')->attrs('src');
        $data = QueryList::get('https://www.bilibili.com/video/BV1NY4y1A7PU?spm_id_from=333.851.b_7265636f6d6d656e64.1')->find('img')->attrs('src');

        //打印结果
        print_r($data->all());

        //采集某页面所有的超链接和超链接文本内容
        //可以先手动获取要采集的页面源码
        $html = file_get_contents('http://cms.querylist.cc/google/list_1.html');
        //然后可以把页面源码或者HTML片段传给QueryList
        $data = QueryList::html($html)->rules([ //设置采集规则
            // 采集所有a标签的href属性
            'link' => ['a','href'],

            // 采集所有a标签的文本内容
            'text' => ['a','text'] ])->range('li')->query()->getData();
        //打印结果
        print_r($data->all());
    }

    public function zcode(){
        $arr = [
            'page' => 10,
            'limit' => 1
        ];    //请求参数


        $hearders = [
            'headers' => [
                'token' => 'eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxNDEwNDI2MTU0MDM5NDMxMTY5IiwiaWF0IjoxNjI2ODUwMjcwLCJzdWIiOiJkNDkzOTJmNWYyOWI4YjMxNzI0OGEwMmY3MzY3Yjk4NDlhZmRjZGY1IiwiZXhwIjoxNjI5NDQyMjcwfQ.ie5Kw9n3NSz3qPsBNxj30ztBvUUDD0zkOAt-ls8dS2E'
            ]
        ];   //设置请求头，根据不同网站设置

        $mes = QueryList::get('https://www.baidu.com/',$arr,$hearders);
        $res = json_decode($mes->getHtml(),true); //如果是抓接口，记得encode,
        return json_encode($res);
    }

//    原生爬虫
    public function getcurlcode()
    {
        header( "Content-type:text/html;Charset=utf-8" );
        $ch = curl_init();
        $url ="https://www.baidu.com";
        curl_setopt ( $ch , CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36" );
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content=curl_exec($ch);
//        正则匹配出一样的html
        preg_match_all("/<td rowspan=\"\d\">(.*?)<\/td>\n<td rowspan=\"\d\">(.*?)<\/td><td rowspan=\"\d\" align=\"\w+\">(.*?)<\/td><td rowspan=\"\d\" align=\"\w+\">(.*?)<\/td><td>(.*?)<\/td>\n<td>(.*?)<\/td><td>(.*?)<\/td>/",$content,$matchs,PREG_SET_ORDER);//匹配该表所用的正则
        var_dump($matchs);


//        原生爬取图片
//        header( "Content-type:text/html;Charset=utf-8" );
//        $ch = curl_init();
//        $url="http://image.baidu.com/";
//        curl_setopt ( $ch , CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36" );
//        curl_setopt($ch,CURLOPT_URL,$url);
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    $content=curl_exec($ch);    $string=file_get_contents($url);
////        正则匹配出一样的html
//        preg_match_all("/<img([^>]*)\s*src=('|\")([^'\"]+)('|\")/",
//            $string,$matches);    $new_arr=array_unique($matches[3]);     foreach($new_arr as $key){
//        echo "<img src=$key>";
//    }


    }


}
